package com.qinsoft.frame.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/** 
 * DateUtils类描述：日期相关工具类
 */
public class DateUtils
{

    public static String YYYYMMDD = "yyyyMMdd";

    public static String getDate()
    {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        return formatter.format(new Date());
    }
    
    public static String formatDate(Date date, String pattern)
    {
        if (date == null || pattern == null) { return null; }

        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }
    public static Date parse(String date, String pattern) throws ParseException
    {
		return new SimpleDateFormat(pattern).parse(date);
	}
    public static Date getDateByString(String str) throws ParseException
    {
		return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(str);
	}
    /**
     * 判断两参数是否为同一天
     * 
     * @param date1
     * @param date2
     * @return
     */
    public static boolean isSameDay(Date date1, Date date2)
    {
        if (date1 == null || date2 == null) { return false; }
        Calendar cal1 = Calendar.getInstance();
        cal1.setTime(date1);
        Calendar cal2 = Calendar.getInstance();
        cal2.setTime(date2);
        return (cal1.get(Calendar.ERA) == cal2.get(Calendar.ERA) && cal1.get(Calendar.YEAR) == cal2.get(Calendar.YEAR) && cal1.get(Calendar.DAY_OF_YEAR) == cal2.get(Calendar.DAY_OF_YEAR));
    }

    /**
     * 判断两参数是否为同一天，参数用毫秒表示时间
     * 
     * @param time1
     * @param time2
     * @return
     */
    public static boolean isSameDay(Long time1, Long time2)
    {
        if (time1 == null || time2 == null)
        {
            return false;
        } else
        {
            return isSameDay(new Date(time1), new Date(time2));
        }
    }

    /**
     * 添加天数,如果date为null取当时时间为date
     * 
     * @param date
     * @param amount
     * @return
     */
    public static Date addDay(Date date, int amount)
    {
        Calendar c = Calendar.getInstance();
        if (date != null)
        {
            c.setTime(date);
        }
        c.add(Calendar.DAY_OF_MONTH, amount);
        return c.getTime();
    }

    /**
     * 添加月份,如果date为null取当时时间为date
     * 
     * @param date
     * @param amount
     * @return
     */
    public static Date addMonth(Date date, int amount)
    {
        Calendar c = Calendar.getInstance();
        if (date != null)
        {
            c.setTime(date);
        }
        c.add(Calendar.MONTH, amount);
        return c.getTime();
    }

    /**
     * 返回某天的分钟数
     * 
     * @param date
     * @return
     */
    public static int getMinuteOfDay(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        if (date != null)
        {
            calendar.setTime(date);
        }
        int nowMinuteOfDay = calendar.get(Calendar.HOUR_OF_DAY) * 60 + calendar.get(Calendar.MINUTE);
        return nowMinuteOfDay;
    }

    /**
     * 一天的最小时间
     * @param date
     * @return
     */
    public static Date getStartTimeOfDay(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        if (date != null)
        {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 一天的最大时间
     * @param date
     * @return
     */
    public static Date getEndTimeOfDay(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        if (date != null)
        {
            calendar.setTime(date);
        }
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        calendar.set(Calendar.MILLISECOND, 999);
        return calendar.getTime();
    }
    /**
     * 临近的半小时
     * @param date
     * @return
     */
    public static Date getHalfTimeOfDay(Date date)
    {
        Calendar calendar = Calendar.getInstance();
        if (date != null)
        {
            calendar.setTime(date);
        }
        if(calendar.get(Calendar.MINUTE)>0&&calendar.get(Calendar.MINUTE)<=30)
        {
            calendar.set(Calendar.MINUTE, 30);
        }
        if(calendar.get(Calendar.MINUTE)>30)
        {
            calendar.set(Calendar.HOUR_OF_DAY, calendar.get(Calendar.HOUR_OF_DAY)+1);
            calendar.set(Calendar.MINUTE, 0);
        }
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }
}
